package components.utils.observer;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionManager <T> implements Subject {
    
    private Collection<T> collection;
    private ArrayList<Observer> observers = new ArrayList<>();

    public CollectionManager(Collection<T> collection) {
        this.collection = collection;
    }

    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    /**
     * It's not necessary to call this method, it gets called automatically
     * after adding or removing objects using addObject and removeObject.
     */
    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.notifyObserver();
        }
    }
    
    public void addObject(T object) {
        collection.add(object);
        notifyObservers();
    }
    
    public void removeObject(T object) {
        collection.remove(object);
        notifyObservers();
    }

    public Collection<T> getCollection() {
        return collection;
    }

    public void setCollection(Collection<T> collection) {
        this.collection = collection;
    }

}
